// pages/classPlan/classPlan.js
let http = require('../../utils/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '课程安排',
    active: "1",
    indexNum: 0,
    projectIndex: 0,
    projectArray: [],
    page: 1,
  },
  // 项目筛选
  ProjectChange: function (e) {
    let _this = this
    let projectid = _this.data.projectArray[e.detail.value].id
    this.setData({
      projectIndex: e.detail.value,
      indexNum: 1,
    })
    http.getHttp('/user_course_data', {
      status: _this.data.status,
      project_id: projectid
    }, {}).then(res => {
      let projectTmp = res.data.data
      this.setData({
        classTmp: projectTmp
      })
    })

  },
  // TAB顶部
  onChange(event) {
    let status = event.detail.name
    this.setData({
      result: event.detail,
      status
    });
    http.getHttp('/user_course_data', {
      status: status
    }, {
      token: wx.getStorageSync('token')
    }).then(res => {
      let tmp = res.data.data
      this.setData({
        classTmp: tmp,
        indexNum: 0
      })
    })
  },
  // 请假
  askLeave(e) {
    let course_id = e.currentTarget.dataset.vavateid
    let cCourse = e.currentTarget.dataset.course_name
    let cCoach = e.currentTarget.dataset.coach_name
    let cStime = e.currentTarget.dataset.start_times
    let cSdates = e.currentTarget.dataset.start_dates
    console.log(cCourse)
    console.log(cCoach)
    console.log(cStime)
    console.log(cSdates)
    wx.navigateTo({
      url: `/pages/classLeave/classLeave?course_id=${course_id}&cCourse=${cCourse}&cCoach=${cCoach}&cStime=${cStime}&cSdates=${cSdates}`,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 初始请求已上课程
    http.getHttp('/user_course_data', {
      status: 1
    }, {
      token: wx.getStorageSync('token')
    }).then(res => {
      let tmpArr = res.data.data
      this.setData({
        classTmp: tmpArr
      })
    })
    // 课时数据
    http.getHttp('/user_course_period', {}, {}).then(res => {
      let courseTimeTmp = res.data.data
      this.setData({
        tmp: courseTimeTmp
      })
    })
    // 请求项目picker
    http.getHttp('/user_project_data', {}, {}).then(res => {
      console.log(res, '项目列表')
      let projectTmp = res.data.data
      this.setData({
        projectArray: projectTmp
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    let hot = that.data.hot
    //上拉加载
    wx.showLoading({
      title: '正在加载',
    })

    //加载次数加一
    this.setData({
      page: this.data.page + 1
    })

    http.getHttp('/user_course_data', {
      page: that.data.page,
      status: that.data.status
    }, {}).then(res => {
      console.log(res)
      debugger
      // console.log('加载更多', res)
      if (res.data.code == 200 && res.data.data.length !== 0) {
        hot = hot.concat(res.data.data)
        //往前台传递数据
        that.setData({
          classTmp
        })
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      } else {
        wx.showToast({
          title: '没有更多了喔~',
          icon: 'none'
        })
      }
    })
    setTimeout(function () {
      wx.hideLoading()
    }, 1000)

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})